<?php

namespace api\modules\v1\controllers\rbac;

use api\controllers\OnAuthController;
use api\modules\v1\forms\rbac\RoleForm;
use common\helpers\CommonHelper;
use common\models\rbac\Role;
use services\rbac\RolePermissionService;
use services\rbac\RoleService;
use Yii;
use yii\web\HttpException;
use Exception;

/**
 * Class PersonnelController
 * @package api\modules\v1\controllers\rbac
 */
class RoleController extends OnAuthController
{
    public $modelClass = Role::class;

    /**
     * @api {get} /v1/rbac/role/list 角色列表
     * @apiName list
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiParam {Number} page                           页码
     * @apiParam {Number} pageSize                       每页显示数量
     *
     * @apiSuccess {Object}  data                        返回数据
     * @apiSuccess {Number}  data.page                   当前页码
     * @apiSuccess {Number}  data.pageSize               每页显示数
     * @apiSuccess {Number}  data.total                  总数
     * @apiSuccess {Array}   data.list                   列表
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        // 查询结果
        $service = new RoleService();
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化
        $data['list'] = $service->getListPageFormat($data['list']);

        return $data;
    }

    /**
     * @api {get} /v1/rbac/role/info 角色详情
     * @apiName info
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                        角色ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object}  data                   返回数据
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new RoleService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {get} /v1/rbac/role/permission 角色权限
     * @apiName permission
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiParam {Number} id                        角色ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object}  data                   返回数据
     */
    public function actionPermission()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        $rolePermissionService = new RolePermissionService();
        return $rolePermissionService->getRoleAuth($id);
    }

    /**
     * @api {post} /v1/rbac/role/create 新增角色
     * @apiName create
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiBody {String} name              角色名称
     * @apiBody {String} desc              说明
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new RoleForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new RoleService();
        return $service->create($model);
    }

    /**
     * @api {post} /v1/rbac/role/update 编辑角色
     * @apiName update
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                角色ID
     * @apiBody {String} name              角色名称
     * @apiBody {String} desc              说明
     * @apiBody {Number} status            菜单状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         角色ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new RoleForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new RoleService();
        return $service->edit($model);
    }

    /**
     * @api {post} /v1/rbac/role/update-permission 编辑角色权限
     * @apiName update-permission
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                角色ID
     * @apiBody {Array}  menu_ids          菜单IDs
     * @apiBody {Array}  action_ids        动作IDs
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         角色ID
     */
    public function actionUpdatePermission()
    {
        $params = Yii::$app->request->post();

        $model = new RoleForm();
        $model->setScenario('edit_permission');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new RoleService();
        return $service->editPermission($model);
    }

    /**
     * @api {post} /v1/rbac/role/delete 删除角色
     * @apiName delete
     * @apiGroup RbacRole
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                角色ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         角色ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new RoleForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new RoleService();
        return $service->delete($model);
    }
}
